package com.zzsy.carosshopprojects.util;

import android.content.Context;
import android.widget.ImageView;

import com.kaopiz.kprogresshud.KProgressHUD;

/**
 * Created by Administrator on 2018/7/2 0002.
 * Create name : wangkuo
 * Explain : 网络加载 样式
 */

public class HUD {

    private volatile static HUD instance;

    //是否现实
    private boolean isShow;

    // 网络加载progress
    private KProgressHUD hud;

    //无参构造方法
    private HUD() {

    }

    public static HUD getInstance() {
        if (instance == null) {
            synchronized (HUD.class) {
                if (instance == null) {
                    instance = new HUD();
                }
            }
        }
        return instance;
    }

    /**
     * 设置默认显示
     */
    public void show(Context context) {
        if (!isShow) {
            try {
                hud = KProgressHUD.create(context)
                        .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                        .setLabel("我正在努力加载...")
                        .setCancellable(false)
                        .setAnimationSpeed(2)
                        .setDimAmount(0.5f)
                        .show();
            } catch (Exception e) {
                e.printStackTrace();
            }
            isShow = true;


        }
    }

    /**
     * 设置自定义message
     */
    public void show(Context context, String msg) {
        if (!isShow) {
            hud = KProgressHUD.create(context)
                    .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                    .setLabel(msg)
                    .setAnimationSpeed(2)
                    .setCancellable(false)
                    .setDimAmount(0.5f)
                    .show();
            isShow = true;
        }
    }

    /**
     * 设置自定义消息 以及图片
     */
    public void show(Context context, int res, String msg) {
        if (!isShow) {
            ImageView imageView = new ImageView(context);
            imageView.setImageResource(res);
            hud = KProgressHUD.create(context)
                    .setCustomView(imageView)
                    .setLabel(msg)
                    .setCancellable(false)
                    .setAnimationSpeed(2)
                    .setDimAmount(0.5f)
                    .show();
            isShow = true;

        }
    }

    /**
     * 将progress 进行隐藏
     * */
    public void dismiss(){
        try {
            if(hud != null){
                hud.dismiss();
                isShow = false;
            }
        }catch (IllegalArgumentException e){
            e.printStackTrace();
        }

    }

}
